<?php
namespace AuctionCore\CarDealer;

use AuctionCore\AuctionOrder\AuOrderList;
use AuctionCore\CarResource\CarModel;
use AuctionCore\Other\CouponList;

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//


/**
 * "买到的车"列表
 * @author       爱民
 */
class BuyCarList extends AuOrderList
{

    private $dealer_id = null;

    private $base_sql = null;
    private $count_sql = null;

    public function __construct($dealer_id)
    {
        $this->dealer_id = $dealer_id;

        $query_sql = "SELECT c.model_id,c.car_color,c.plate_prefix,c.plate_suffix,t.* ";
        $sql = " FROM (
               SELECT a.man_id,a.take_method,b.order_id,b.order_no,b.car_id,b.coupon,b.dealer_confirm_status
                   ,b.status,b.trade_time,b.bid_best_price,b.bidding_best_price
                   ,b.success_price,b.commision,b.transfer_fee,b.deliver_type,b.pay_type
                   FROM au_dealer_auctioned a LEFT JOIN au_order b 
                   ON a.order_id = b.order_id 
             WHERE a.status=0 AND a.dealer_id={$dealer_id}
             ) t LEFT JOIN au_cars c
            ON t.car_id = c.car_id";
        $this->base_sql = $query_sql . $sql;
        $this->count_sql = "select count(*) count " . $sql;
    }


    /**
     * 获取列表结果
     * @param    int $ev_page_num 每页条数
     * @param    int $page 页码
     * @return   Object
     */
    public function get($ev_page_num, $page)
    {

        $dealer_id = $this->dealer_id;
        $Model = new \Think\Model();

        //拍单列表信息
        $sql = $this->base_sql;
        $sql .= " {$this->condition}";
        $skip = ($page < 2 ? 0 : $ev_page_num * ($page - 1));
        $sql .= " limit {$skip},{$ev_page_num}";
        $sql .= ";";
        //echo $sql;exit;
        $list = $Model->query($sql);

        $cm = new CarModel();
        foreach ($list as $key => $row) {
            //车型信息
            $list[$key]['model_info'] = $cm->get_parent_brand_info($row['model_id']);

            //过户信息
            $list[$key]['task_man_info'] = $Model->query(
                "SELECT e.real_name,t.*
             FROM (
                SELECT emp_id,company,phone,contact,idcard 
                FROM au_transfer_log 
                WHERE order_id = '{$row['order_id']}'
             ) t LEFT JOIN au_employee e 
            ON t.emp_id = e.emp_id LIMIT 1;");
        }
        return $list;
    }

    /**
     * 获取各状态下拍品数量
     * @param array $params
     * @return int count
     * @author angl
     */
    public function getCount($params)
    {
        $map = array();
        $map['ada.dealer_id'] = array('eq', $this->dealer_id);
        //成交待确认
        if ($params['status'] ==1) {
            $map['ada.status'] = array('eq', 0);
            $map['ao.status'] = array('in', array(5, 7));
        }
        //待付款
        if ($params['status'] == 2) {
            $map['ada.status'] = array('eq', 0);
            $map['ao.status'] = array('eq', 8);
        }
        //待过户
        if ($params['status'] == 3) {
            $map['ada.status'] = array('eq', 0);
            $map['ao.status'] = array('in', array(9, 10));
        }
        //交易成功
        if ($params['status'] == 4) {
            $map['ada.status'] = array('eq', 0);
            $map['ao.status'] = array('in', array(11, 13));
        }
        //交易失败
        if ($params['status'] == 5) {
            //$map['ao.status'] = array('eq', 12);
            $map['_string'] = "(ada.status =0 and ao.status=12) or ada.status>0";
        }

        $prefix = C("DB_PREFIX");
        $total = M('dealer_auctioned')->alias("ada")
            ->join("{$prefix}order as ao ON ada.order_id=ao.order_id")
            ->join("{$prefix}cars as ac  ON ao.car_id=ac.car_id")
            ->where($map)->count();

        return $total;
    }

    /**
     * 获取列表
     * @param $page
     * @param $size
     * @param array $params
     * @return array
     */
    public function getList($page, $size, $params = array())
    {
        $page = (int)$page < 2 ? 1 : (int)$page;
        $size = (int)$size < 1 ? 10 : (int)$size;
        //搜索条件
        $map = array();
        $map['ada.dealer_id'] = array('eq', $this->dealer_id);
        //成交待确认
        if ($params['status'] ==1) {
            $map['ada.status'] = array('eq', 0);
            $map['ao.status'] = array('in', array(5, 7));
        }
        //待付款
        if ($params['status'] == 2) {
            $map['ada.status'] = array('eq', 0);
            $map['ao.status'] = array('eq', 8);
        }
        //待过户
        if ($params['status'] == 3) {
            $map['ada.status'] = array('eq', 0);
            $map['ao.status'] = array('in', array(9, 10));
        }
        //交易成功
        if ($params['status'] == 4) {
            $map['ada.status'] = array('eq', 0);
            $map['ao.status'] = array('in', array(11, 13));
        }
        //交易失败
        if ($params['status'] == 5) {
            $map['_string'] = "(ada.status =0 and ao.status=12) or ada.status>0";
        }

        if ($params['brand_id'] > 0) {
            $map['ada.status'] = array('eq', 0);
            $map['ac.brand_id'] = array('eq', $params['brand_id']);
        }

        if ($params['ac.series_id'] > 0) {
            $map['ada.status'] = array('eq', 0);
            $map['ac.series_id'] = array("eq", $params['series_id']);
        }
        //成交时间
        if (!empty($params['trade_time'])) {
            $map['ada.status'] = array('eq', 0);
            $map['ao.trade_time'] = array("between", $params['trade_time'] . "00:00:00", $params['trade_time'] . "23:59:59");
        }
        //交付方式
        if ($params['deliver_type'] > 0) {
            $map['ada.status'] = array('eq', 0);
            $map['ao.deliver_type'] = array('eq', $params['deliver_type']);
        }
        //支付方式
        if ($params['pay_type'] > 0) {
            $map['ada.status'] = array('eq', 0);
            $map['ao.pay_type'] = array('eq', $params['pay_type']);
        }

        $cols = "ada.man_id,ada.dealer_id,ada.take_method,ada.order_id,ada.status ada_status,ada.trade_price,ao.trade_time,
            ao.order_id,ao.order_no,ao.status,ao.car_id,ao.coupon,ao.dealer_confirm_status,ao.dealer_pay_status,ao.bid_best_price,
            ao.bidding_best_price,ao.commision,ao.transfer_fee,ao.deliver_type,ao.pay_type,ao.bidding_end_time,
            ac.car_id,ac.car_no,ac.mbrand_id,brand_id,series_id,model_id,ac.car_color,ac.plate_suffix,ac.plate_prefix,ac.car_cc,
            ac.turbo,ac.gearbox,ac.transfer_limit";

        $prefix = C("DB_PREFIX");
        $total = M('dealer_auctioned')->alias("ada")
            ->join("{$prefix}order as ao ON ada.order_id=ao.order_id")
            ->join("{$prefix}cars as ac  ON ao.car_id=ac.car_id")
            ->where($map)->count();
        $list = M('dealer_auctioned')->alias("ada")->field($cols)
            ->join("{$prefix}order as ao ON ada.order_id=ao.order_id")
            ->join("{$prefix}cars as ac  ON ao.car_id=ac.car_id")
            ->where($map)->order("ao.trade_time")->limit($size)->page($page)->select();
        if (!empty($list)) {
            $cm = new CarModel();
            $Coupon = new CouponList();
            $Raffle = new \AuctionCore\Other\Raffle();
            foreach ($list as $key => $val) {
                $series = $cm->get_model_by_id($val['series_id'], 'series');
                $list[$key]['model'] = $series['name'] . ' ' . $val['car_cc'] . ($val['turbo'] ? 'T' : 'L') . ' ' . C('GEARBOX.' . $val['gearbox']);

                $coupon_money = $Coupon->getSumCouponCost($this->dealer_id, $val['order_id']);
                $list[$key]['coupon_money'] = ($val['coupon'] <= 0) ? '0' : ($coupon_money > $val['coupon'] ? $val['coupon'] : $coupon_money);

                //抽奖
                $Raffle_money = $Raffle->find_cards(array('order_id'=>$val['order_id']));
                $list[$key]['raffle_money'] = $Raffle_money[0]['price'];
                $list[$key]['raffle_state'] = $Raffle_money[0]['state'];
                $Raffle_money = $Raffle_money[0]['state']>0?$Raffle_money[0]['price']:0;

                //应付总额
                $list[$key]['total_money'] = $val['trade_price'] + $val['commision'] + $val['transfer_fee'] - $list[$key]['coupon_money'] - $Raffle_money;

                //是否用过代金卷
                $map = array();
                $map['order_id'] = array('eq', $val['order_id']);
                $map['dealer_id'] = array('eq', $this->dealer_id);
                $map['occur_type'] = array('eq', 1);
                $flag = M('coupon_log')->where($map)->find();
                if ($flag) {
                    $list[$key]['coupon_used'] = true;
                } else {
                    $list[$key]['coupon_used'] = false;
                }

            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /*
     * 获取我的代金卷列表
     * */
    public function get_coupon_list()
    {
        $map = array();
        $map['dealer_id'] = array('eq', $this->dealer_id);
        $map['use_status'] = array('eq', '0');
        $map['start_date'] = array('elt', date('Y-m-d'));
        $map['end_date'] = array('egt', date('Y-m-d'));
        $res = M('dealer_coupon')->where($map)->order("end_date")->select();
        return $res;
    }

    public function count()
    {
        $Model = new \Think\Model();
        $sql = $this->count_sql;
        $sql .= " {$this->condition}";
        $sql .= ";";
        $r = $Model->query($sql);
        return $r[0]['count'];
    }

}

?>